/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.entity.BidItem;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  获取item列表
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidItemListPlugin implements PluginConnector {

	private IDao bidItemDao;
	private Logger logger = Logger.getLogger(BidItemListPlugin.class);

	/**
	 * TODO 获取item列表插件<br> 
	 * bidItem属性:按item属性获取item列表<br> 
	 * 分页设置:<br>
	 * pageSize:每页记录数;<br>
	 * pageIndex:当前页码 
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			//单独处理的特殊参数情况[暂无]
			//-------------------
			MethodParam param = setMethodParams(vo, 2, "");
			if (Validator.isEmpty(param)) {
				result.setResult("false");
				result.setMessage("查询条件为空!");
				return result;
			}
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			param.setResultCache(false);
			String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
					" order by close_time_pre asc,abs(order_number) asc");
			param.setParams("orderBy", orderBy);
			if (pageSize == 0 && pageIndex == 0) {
				List list = bidItemDao.select(param);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				Page page = bidItemDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			logger.error("执行bidItem查询失败 ", e);
			result.setMessage("bidItem查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		MethodParam param = new MethodParam("ByProperty", "", "", BidItem.class.getName());
		param.setResultCache(false);
		String all = String.valueOf(vo.getParams("bidItemAll"));//获取所有活动列表
		if (Validator.isNull(all)) {
			getBidItemParams(param, vo);
			if (Validator.isEmpty(param.getParams()) || param.getParams().size() == 0)
				return null;
		}
		return param;
	}

	//组织bidItem信息查询条件
	private void getBidItemParams(MethodParam param, ParamsVo vo) {
		BidItem bidItem = new BidItem();
		bidItem = (BidItem) WbdToolsUtil.getInstance().convert2Bean(vo.getParams(), bidItem);
		if (Validator.isNotNull(bidItem.getItemId())) {
			param.setParams("itemId", bidItem.getItemId());
		}
		if (Validator.isNotNull(bidItem.getSessionId())) {
			param.setParams("sessionId", bidItem.getSessionId());
		}
		if (Validator.isNotNull(bidItem.getSessionType())) {
			param.setParams("sessionType", bidItem.getSessionType());
		}
		if (Validator.isNotNull(bidItem.getSessionName())) {
			param.setParams("sessionName", bidItem.getSessionName());
		}
		if (Validator.isNotNull(bidItem.getGroupId())) {
			param.setParams("groupId", bidItem.getGroupId());
		}
		if (Validator.isNotNull(bidItem.getGroupName())) {
			param.setParams("groupName", bidItem.getGroupName());
		}
		if (Validator.isNotNull(bidItem.getBidMode())) {
			param.setParams("bidMode", bidItem.getBidMode());
		}
		if (Validator.isNotNull(bidItem.getIfBidDyn())) {
			param.setParams("ifBidDyn", bidItem.getIfBidDyn());
		}
		if (Validator.isNotNull(bidItem.getItemNo())) {
			param.setParams("itemNo", bidItem.getItemNo());
		}
		if (Validator.isNotNull(bidItem.getItemName())) {
			param.setParams("itemName", " like '%" + bidItem.getItemName() + "%' ");
		}
		if (Validator.isNotNull(bidItem.getItemType())) {
			param.setParams("itemType", bidItem.getItemType());
		}
		if (Validator.isNotNull(bidItem.getItemTypeName())) {
			param.setParams("itemTypeName", bidItem.getItemTypeName());
		}
		if (Validator.isNotNull(bidItem.getItemSubname())) {
			param.setParams("itemSubname", bidItem.getItemSubname());
		}
		if (Validator.isNotNull(bidItem.getItemStatus())) {
			param.setParams("itemStatus", bidItem.getItemStatus());
		}
		if (Validator.isNotNull(bidItem.getItemNum())) {
			param.setParams("itemNum", bidItem.getItemNum());
		}
		if (Validator.isNotNull(bidItem.getItemNumDesc())) {
			param.setParams("itemNumDesc", bidItem.getItemNumDesc());
		}
		if (Validator.isNotNull(bidItem.getItemUnitName())) {
			param.setParams("itemUnitName", bidItem.getItemUnitName());
		}
		if (Validator.isNotNull(bidItem.getIncrementGroup())) {
			param.setParams("incrementGroup", bidItem.getIncrementGroup());
		}
		if (Validator.isNotNull(bidItem.getIncrementValue())) {
			param.setParams("incrementValue", bidItem.getIncrementValue());
		}
		if (Validator.isNotNull(bidItem.getFirstValuePre())) {
			param.setParams("firstValuePre", bidItem.getFirstValuePre());
		}
		if (Validator.isNotNull(bidItem.getOncevalue())) {
			param.setParams("oncevalue", bidItem.getOncevalue());
		}
		if (Validator.isNotNull(bidItem.getHoldvalue())) {
			param.setParams("holdvalue", bidItem.getHoldvalue());
		}
		if (Validator.isNotNull(bidItem.getHoldNoticeTime())) {
			param.setParams("holdNoticeTime", bidItem.getHoldNoticeTime());
		}
		if (Validator.isNotNull(bidItem.getDecimalCount())) {
			param.setParams("decimalCount", bidItem.getDecimalCount());
		}
		if (Validator.isNotNull(bidItem.getCurrency())) {
			param.setParams("currency", bidItem.getCurrency());
		}
		if (Validator.isNotNull(bidItem.getPriceUnit())) {
			param.setParams("priceUnit", bidItem.getPriceUnit());
		}
		if (Validator.isNotNull(bidItem.getAllowUpdateTimes())) {
			param.setParams("allowUpdateTimes", bidItem.getAllowUpdateTimes());
		}
		if (Validator.isNotNull(bidItem.getBidWay())) {
			param.setParams("bidWay", bidItem.getBidWay());
		}
		if (Validator.isNotNull(bidItem.getPriceFlag())) {
			param.setParams("priceFlag", bidItem.getPriceFlag());
		}
		if (Validator.isNotNull(bidItem.getFlagTime())) {
			param.setParams("flagTime", bidItem.getFlagTime());
		}
		if (Validator.isNotNull(bidItem.getBailPrice())) {
			param.setParams("bailPrice", bidItem.getBailPrice());
		}
		if (Validator.isNotNull(bidItem.getCreateTime())) {
			param.setParams("createTime", bidItem.getCreateTime());
		}
		if (Validator.isNotNull(bidItem.getAuditTime())) {
			param.setParams("auditTime", bidItem.getAuditTime());
		}
		if (Validator.isNotNull(bidItem.getOpenTimePre())) {
			param.setParams("openTimePre", " and open_time_pre = " + bidItem.getOpenTimePre());
		}
		if (Validator.isNotNull(bidItem.getFirstBidTimePre())) {
			param.setParams("firstBidTimePre", bidItem.getFirstBidTimePre());
		}
		if (Validator.isNotNull(bidItem.getCloseTimePre())) {
			param.setParams("closeTimePre", bidItem.getCloseTimePre());
		}
		if (Validator.isNotNull(bidItem.getLimitTime())) {
			param.setParams("limitTime", bidItem.getLimitTime());
		}
		if (Validator.isNotNull(bidItem.getDelayControl())) {
			param.setParams("delayControl", bidItem.getDelayControl());
		}
		if (Validator.isNotNull(bidItem.getDelayUnit())) {
			param.setParams("delayUnit", bidItem.getDelayUnit());
		}
		if (Validator.isNotNull(bidItem.getDelayPeriod())) {
			param.setParams("delayPeriod", bidItem.getDelayPeriod());
		}
		if (Validator.isNotNull(bidItem.getDelayDeadTime())) {
			param.setParams("delayDeadTime", bidItem.getDelayDeadTime());
		}
		if (Validator.isNotNull(bidItem.getDelayPeriodMax())) {
			param.setParams("delayPeriodMax", bidItem.getDelayPeriodMax());
		}
		if (Validator.isNotNull(bidItem.getIfFee())) {
			param.setParams("ifFee", bidItem.getIfFee());
		}
		if (Validator.isNotNull(bidItem.getAllowFirstValue())) {
			param.setParams("allowFirstValue", bidItem.getAllowFirstValue());
		}
		if (Validator.isNotNull(bidItem.getAllowRepeat())) {
			param.setParams("allowRepeat", bidItem.getAllowRepeat());
		}
		if (Validator.isNotNull(bidItem.getAllowEndPrio())) {
			param.setParams("allowEndPrio", bidItem.getAllowEndPrio());
		}
		if (Validator.isNotNull(bidItem.getAllowHallPrio())) {
			param.setParams("allowHallPrio", bidItem.getAllowHallPrio());
		}
		if (Validator.isNotNull(bidItem.getAllowPrioTime())) {
			param.setParams("allowPrioTime", bidItem.getAllowPrioTime());
		}
		if (Validator.isNotNull(bidItem.getAllowAdd())) {
			param.setParams("allowAdd", bidItem.getAllowAdd());
		}
		if (Validator.isNotNull(bidItem.getAllowAddTime())) {
			param.setParams("allowAddTime", bidItem.getAllowAddTime());
		}
		if (Validator.isNotNull(bidItem.getPrioControl())) {
			param.setParams("prioControl", bidItem.getPrioControl());
		}
		if (Validator.isNotNull(bidItem.getEndPrioDays())) {
			param.setParams("endPrioDays", bidItem.getEndPrioDays());
		}
		if (Validator.isNotNull(bidItem.getBidInterval())) {
			param.setParams("bidInterval", bidItem.getBidInterval());
		}
		if (Validator.isNotNull(bidItem.getBidIntervalLimit())) {
			param.setParams("bidIntervalLimit", bidItem.getBidIntervalLimit());
		}
		if (Validator.isNotNull(bidItem.getIsPreview())) {
			param.setParams("isPreview", bidItem.getIsPreview());
		}
		if (Validator.isNotNull(bidItem.getItemSource())) {
			param.setParams("itemSource", bidItem.getItemSource());
		}
		if (Validator.isNotNull(bidItem.getItemBrand())) {
			param.setParams("itemBrand", bidItem.getItemBrand());
		}
		if (Validator.isNotNull(bidItem.getItemArea())) {
			param.setParams("itemArea", bidItem.getItemArea());
		}
		if (Validator.isNotNull(bidItem.getPackId())) {
			param.setParams("packId", bidItem.getPackId());
		}
		if (Validator.isNotNull(bidItem.getProId())) {
			param.setParams("proId", bidItem.getProId());
		}
		if (Validator.isNotNull(bidItem.getSysId())) {
			param.setParams("sysId", bidItem.getSysId());
		}
		if (Validator.isNotNull(bidItem.getPicUrl())) {
			param.setParams("picUrl", bidItem.getPicUrl());
		}
		if (Validator.isNotNull(bidItem.getPicUrlSmall())) {
			param.setParams("picUrlSmall", bidItem.getPicUrlSmall());
		}
		if (Validator.isNotNull(bidItem.getOrgEname())) {
			param.setParams("orgEname", bidItem.getOrgEname());
		}
		if (Validator.isNotNull(bidItem.getOrgCname())) {
			param.setParams("orgCname", bidItem.getOrgCname());
		}
		if (Validator.isNotNull(bidItem.getFallDecrement())) {
			param.setParams("fallDecrement", bidItem.getFallDecrement());
		}
		if (Validator.isNotNull(bidItem.getFallLimitTime())) {
			param.setParams("fallLimitTime", bidItem.getFallLimitTime());
		}
		if (Validator.isNotNull(bidItem.getFallLowerstPrice())) {
			param.setParams("fallLowerstPrice", bidItem.getFallLowerstPrice());
		}
		if (Validator.isNotNull(bidItem.getModifyTime())) {
			param.setParams("modifyTime", bidItem.getModifyTime());
		}
		if (Validator.isNotNull(bidItem.getNetBidKind())) {
			param.setParams("netBidKind", bidItem.getNetBidKind());
		}
		if (Validator.isNotNull(bidItem.getOrderNumber())) {
			param.setParams("orderNumber", bidItem.getOrderNumber());
		}
		if (Validator.isNotNull(bidItem.getSpare1())) {
			param.setParams("spare1", bidItem.getSpare1());
		}
		if (Validator.isNotNull(bidItem.getSpare2())) {
			param.setParams("spare2", bidItem.getSpare2());
		}
		if (Validator.isNotNull(bidItem.getSpare3())) {
			param.setParams("spare3", bidItem.getSpare3());
		}
		if (Validator.isNotNull(bidItem.getSpare4())) {
			param.setParams("spare4", bidItem.getSpare4());
		}
		if (Validator.isNotNull(bidItem.getSpare5())) {
			param.setParams("spare5", bidItem.getSpare5());
		}
		if (Validator.isNotNull(bidItem.getIfRight())) {
			param.setParams("ifRight", bidItem.getIfRight());
		}
		if (Validator.isNotNull(bidItem.getvFlag())) {
			param.setParams("vflag", bidItem.getvFlag());
		}
		if (Validator.isNotNull(bidItem.getDirection())) {
			param.setParams("direction", bidItem.getDirection());
		}
	}

	public void setBidItemDao(IDao bidItemDao) {
		this.bidItemDao = bidItemDao;
	}

}
